var instanceId;
var instanceInfo;
$(function () {
    instanceId=request("iId");

    $('#btnBackList').on('click', function () {
        history.back();
    });

    var service = {};
    service.id=instanceId;
    var fn="queryUserDiyDetail";
    service = Commonjs.jsonToString(service)
    var params = Commonjs.getParams(fn,service);//获取参数
    Commonjs.ajaxTrue(weburl,params,function (data) {
        var info=data.data;
        vmDiyProductUpgrade.bizInfo = info;
        vmDiyProductUpgrade.productParam = info.productParam;
        vmDiyProductUpgrade.listen();
        $('#diyForm').attr('src', info.productParam.paramUpdateUrl);
    });
});

var vmDiyProductUpgrade = new Vue({
    el: '#MainContentDIV',
    data: function () {
        return {
            sendData: {},
            productParam: {
                paramUpdateUrl: '',
                paramUpdateHeight: 400
            },
            bizInfo: {}
        }
    },
    methods: {
        listen: function () {
            var cloudMessage = new CloudMessage();
            var self = this;
            cloudMessage.addListen('diyBizInfoQuery', function (message) {
                console.log("接收到业务数据获取请求");
                var messageType = 'diyBizInfoQueryResponse';
                var data = {
                    business_id: self.bizInfo.id,
                    start_time: jsonDateTimeFormat(self.bizInfo.startTime),
                    end_time: jsonDateTimeFormat(self.bizInfo.endTime),
                    product_code: self.bizInfo.productCode,
                    product_name: self.bizInfo.productName,
                    sub_class: self.bizInfo.subClass,
                    sub_class_name: self.bizInfo.subClassName,
                    status: self.bizInfo.status,
                    user_id: self.bizInfo.userId,
                    open_info: self.bizInfo.openInfo,
                    instance_info: self.bizInfo.instanceInfo
                };
                cloudMessage.send(document.getElementById('diyForm'), messageType, data);
            }).addListen('diyUpgradeOrder', function (message) {
                if (!message.order_describe || "" == message.order_describe || message.order_now == undefined || message.order_now == null)  {
                    self.$message.error('基础参数不正确');
                    return;
                }

                var service = {};
                service.id = self.bizInfo.id;
                service.order_describe = message.order_describe
                delete message.order_describe;
                service.diy_params = message;

                //提交订单
                var fn="addUserCartDiyUpgrade";
                service = JSON.stringify(service);
                var params = Commonjs.getParams(fn,service);
                Commonjs.ajaxTrue(weburl,params,function (data) {
                    if (message.order_now == 1) {
                        window.open(Commonjs.getCfgVal(configParam.common.cfgKey.template) + "/usercenter/shopping/shoppinglist.html");
                    } else {
                        self.$message.success("已添加到购物车");
                        queryUserCartCount();
                    }
                },true,"正在加入购物车中...");

            }).addListen('diyUpgradePriceQuery', function (data) {
                var service = data;
                service.id = self.bizInfo.id;
                service.business_id = self.bizInfo.id;

                var fn="getDiyUpdatePrice";
                service = Commonjs.jsonToString(service);
                var params = Commonjs.getParams(fn,service);//获取参数

                $.ajax({
                    datatype:"json",
                    type:"POST",
                    url: weburl,
                    data:params,
                    cache : false,
                    success: function(priceData){
                        if ("string" == typeof priceData) {
                            priceData = JSON.parse(priceData);
                        }
                        if (priceData.result == "success") {
                            cloudMessage.send(document.getElementById('diyForm'), 'diyUpgradePriceQueryResponse', priceData.data);
                        } else {
                            self.$message.error(priceData.msg);
                        }
                    }
                });
            });
            cloudMessage.startListen(Commonjs.getHostUrlFromString(self.productParam.paramUpdateUrl));
        }
    },
    mounted: function () {
    }
});
